function contains(a, key) {
    for (var i = 0; i < a.length; i++) {
        // alert("a[i]:"+ a[i] +" key:"+ key);
        if (a[i] == key) {
            return true;
        }
    }
    return false;
}
function loadUrl(url, name, value) {
    // navigator.app.loadUrl("file:///android_asset/www/"+url);
    // navigator.app.loadUrl(url);
    // window.location.href=url;
    if (!empty(name)) {
        setCookie(name, value);
    }
    window.location = url;
}

function openUrl(url) {
    // alert("openUrl("+url+")");
    window.location = url;
}

function setCookie(c_name, c_value) {
    window.localStorage.setItem(c_name, c_value);
}

function getCookie(c_name) {
    var val = window.localStorage.getItem(c_name);
    if (empty(val)) {
        val = "";
    }
    return val;
}
function clsCookie(c_name) {
    try {
        window.localStorage.removeItem(c_name);
    } catch (err) {
        // 在此处理错误
    }
}

function clsAllCookie(b_name) {
    try {
        var storage = window.localStorage;
        for (var i = 0; i < storage.length; i++) {
            var k = storage.key(i);
            if (startsWith(k, b_name)) {
                // alert("clsAllCookie:" + k);
                clsCookie(k);
            }
        }
    } catch (err) {
        // 在此处理错误
    }
}

function startsWith(s, b) {
    return s.indexOf(b) == 0;
}

function empty(v) {
    switch (typeof v) {
        case 'undefined':
            return true;
        case 'string':
            if (trim(v).length == 0)
                return true;
            break;
        case 'boolean':
            if (!v)
                return true;
            break;
        case 'number':
            if (0 === v)
                return true;
            break;
        case 'object':
            if (null === v)
                return true;
            if (undefined !== v.length && v.length == 0)
                return true;
            for (var k in v) {
                return false;
            }
            return true;
            break;
    }
    return false;
}

//正整数检查
function isPositiveNum(s){//是否为正整数 
    var re = /^[0-9]*[1-9][0-9]*$/ ; 
    return re.test(s) 
} 

function trim(str) {
    for (var i = 0; i < str.length && str.charAt(i) == "  "; i++)
        ;
    for (var j = str.length; j > 0 && str.charAt(j - 1) == "  "; j--)
        ;
    if (i > j)
        return "";
    return str.substring(i, j);
}

function stringToJson(stringValue) {
    eval("var theJsonValue = " + stringValue);
    return theJsonValue;
}

function showLoading() {
    $('#waitingId').modal('show');
}

function hideLoading() {
    $('#waitingId').modal('hide');
}

function openWebUrl(url) {
    navigator.notification.confirm(
            '请确认是否打开网页链接[' + url + ']?', // message
            function (button) {
                // if press 'Yes'
                if (button === 1) {
                    window.open(url, '_blank', 'location=no');
                }
            }, // callback function
            '系统提示', // title
            '是,否'                           // confirm 选项，用逗号隔开
            );

}


function scan() {
    // alert("scan");
    BarcodeScanner.scan({
        success: function (result) {
            // alert(result);
            loadUrl("txtView.html", "tmp_scan_data", result);
        },
        failure: function (err) {
            // alert(err);
        }
    });
}

function exitFromApp()
{
    navigator.notification.confirm(
            '确定要退出程序吗?', // message
            onConfirm, // callback function
            '系统提示', // title
            '是,否'                           // confirm 选项，用逗号隔开
            );
}

function onConfirm(button) {
    // if press 'Yes'
    if (button === 1) {
        navigator.app.exitApp();
    }
}

function alertMessage(msg) {
    //navigator.notification.alert(msg,  function(){}, "提示信息", "确定");  
    alert(msg);
}

function strtoarray(data) {
    if (empty(data)) {
        return new Array();
    } else {
        return data.split(',');
    }
}

function arraytostr(data) {
    var str = "";
    for (var i = 0; i < data.length; i++) {
        // alert("a[i]:"+ a[i] +" key:"+ key);
        if (str == "") {
            str = data[i];
        } else {
            str = str + "," + data[i];
        }
    }
}

function logout() {
    $.ajax({//一个Ajax过程
        type: "post", //以post方式与后台沟通
        url: getServerUrl() + "/Open/User/logout", //与此php页面沟通
        dataType: 'json', //从php返回的值以 JSON方式 解释
        data: '',
        success: function (json) {//如果调用php成功
            if (json.result == "1") {
                window.localStorage.clear();
                openUrl("login.html");
            }
        }
    });
}
function filelogout() {
    $.ajax({//一个Ajax过程
        type: "post", //以post方式与后台沟通
        url: getServerUrl() + "/Open/User/logout", //与此php页面沟通
        dataType: 'json', //从php返回的值以 JSON方式 解释
        data: '',
        success: function (json) {//如果调用php成功
            if (json.result == "1") {
                window.localStorage.clear();
                openUrl("filelogin.html");
            }
        }
    });
}
function isOnline() {
    var img = new Image();
    img.id = "test_is_online";
    img.onload = function () {
        document.body.removeChild(
                document.getElementById("test_is_online"));
    };
    img.onerror = function () {
        document.body.removeChild(
                document.getElementById("test_is_online"));
        alert("请检查网络连接....");
    };
    img.src = "http://www.baidu.com/img/baidu_jgylogo3.gif";
    img.style.display = "none";
    document.body.appendChild(img);
}

function checkAppVersion() {
    //alert("checkAppVersion()");
    UpdatePlugin.checkVesion(getServerUrl() + "/chkupdate", "wms.js", "wms.apk", function (data) {
    }, function (reason) {
    });
}

function quickCheckAppVersion() {
    //alert("checkAppVersion()");
    UpdatePlugin.quitCheckVesion(getServerUrl() + "/chkupdate", "wms.js", "wms.apk", function (data) {
    }, function (reason) {
    });
}

function autoCheckAppVersion() {
    var img = new Image();
    img.id = "test_is_online";
    img.onload = function () {
        document.body.removeChild(
                document.getElementById("test_is_online"));
        quickCheckAppVersion();
    };
    img.onerror = function () {
        document.body.removeChild(
                document.getElementById("test_is_online"));
        alert("请检查网络连接....");
    };
    img.src = "http://www.baidu.com/img/baidu_jgylogo3.gif";
    img.style.display = "none";
    document.body.appendChild(img);
}

var isContinueSync = false;
function sync() {
    var img = new Image();
    img.id = "test_is_online";
    img.onload = function () {
        document.body.removeChild(document.getElementById("test_is_online"));
        showLoading();
        $("#homepagemessageid").html("");
        $("#homepagemessageid").show();
        isContinueSync = true;

        upCompanyToServer();
        //if(isContinueSync) {
        upAllShipItemToServer();
        //}

        //if(isContinueSync) {
        loadCompany();
        //}

        //if(isContinueSync) {
        loadProduct();
        //}
        //hideLoading();

    };
    img.onerror = function () {
        document.body.removeChild(
                document.getElementById("test_is_online"));
        hideLoading();
        alert("请检查网络连接....");
    };
    img.src = "http://www.baidu.com/img/baidu_jgylogo3.gif";
    img.style.display = "none";
    document.body.appendChild(img);
}

function isNumber(oNum)
{
    if (!oNum)
        return false;
    var strP = /^\d+(\.\d+)?$/;
    if (!strP.test(oNum))
        return false;
    try {
        if (parseFloat(oNum) != oNum)
            return false;
    }
    catch (ex)
    {
        return false;
    }
    return true;
}

function getNewCode(content, index) {
    var code1 = content.substring(0, 6);
    var code2 = content.substring(8);
    var newcode2 = parseInt(code2);
    newcode2 = newcode2 + index;
    code2 = newcode2.toString();
    var crc = getCrc(code1 + code2);

    return code1 + crc + code2;
}

function getNewCodeByValue(v) {
    var codePrefix = getCodePrefix();
    var tmpv=""+v;
    while (tmpv.length < 8) {
        tmpv = "0" + tmpv;
    }
    var crc = getCrc(codePrefix + tmpv);

    //alert("codePrefix:" + codePrefix +"  crc:"+ crc +" v:" + tmpv);
    return codePrefix + crc +tmpv;
}

function getCrc(code) {
    var index = 0;
    var count = 0;
    while (code.length > index)
    {
        hex = code.substring(index, index + 1);
        value = parseInt(hex);
        count = count + value * 3;
        index = index + 1;
    }
    var hCount = count.toString();
    return hCount.substring(hCount.length - 1);
}

function getYYYYMMDDDate() {
    var today = new Date();
    var day = today.getDate();
    var month = today.getMonth() + 1;
    var year = today.getFullYear();
    var date = year + "-" + month + "-" + day;
    return date;
}


//语言包初始化 保存到cookie
function L_ini(){
      var Obj_lang = L();//取语言包对象
        //window.Glo_lang = Obj_lang;//对象全局
        var Str_lang=JSON.stringify(Obj_lang);//对象类型 => 字符类型
        setCookie('Str_lang',Str_lang);//cookie只能保存字符类型
        // return Obj_lang;
}

//取得语言包对象
function L_get(){
       Str_lang = getCookie('Str_lang');
        var Obj_lang=JSON.parse(Str_lang);
        return Obj_lang;
}

//模板渲染
function Template_ini(template_name,content_id,obj){

    if(!arguments[2]) obj = {};
    
    $('title').html('溯源客户端1');
    obj.L = "L()";//获取语言包ch.js返回的对象
    var html = template(template_name,obj);
    $(content_id).html(html);
}
function getItemNo(content) {
    //alert("1:" + content);
    if (content != "") {
        /* var li=content.lastIndexOf("?");
        if(li>-1) {
            content=content.slice(li);
        }
        //alert("?:" + content);
        if(content.lastIndexOf(".")>-1) {
            content=content.slice(0,-5);
        }
        //alert(".:" + content);
        li=content.lastIndexOf("/");
        if(li>-1) {
            content=content.slice(li);
        }
        //alert("/:" + content);
        li=content.lastIndexOf(",");
        if(li>-1) {
            content=content.slice(-9);
        }

        if(content.length>=15) {
            content = content.slice(-15);
        } else {
            content = content.slice(-9);
        } */
        return content;
    }
}

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
        }
    }
    return theRequest;
} 

